package homework1;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test1 {
    public static void main(String[] args) {
        //创建学生集合
        List<Student> stuList = new ArrayList<>();
        stuList.add(new Student("古力娜扎", "女", 19, 98));
        stuList.add(new Student("迪丽热巴", "女", 18, 99));
        stuList.add(new Student("周杰伦", "男", 20, 88));
        stuList.add(new Student("蔡徐坤", "男", 19, 78));

        // 按照年龄升序、分数降序排序
        Collections.sort(stuList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                if (s1.getAge() == s2.getAge()) {
                    return Double.compare(s1.getScore(),s2.getScore());
                } else {
                    return s1.getAge() - s2.getAge();
                }
            }
        });

        //定义一个字符输出缓冲流BufferedWriter对象，将学员信息写入到Task1.txt文件中，
        // 每个学员信息占一行，每个字段之间用","符号隔开。例如：
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\Javasepro\\itheima-162sehomework\\Task1.txt"));
            for (int i = 0; i < stuList.size(); i++) {
                bw.write(String.valueOf(stuList.get(i)));
                bw.newLine();
            }
            //刷新
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
